package com.fgr.controller;

import com.fgr.beans.Animal;
import com.fgr.beans.Message;
import com.fgr.beans.User;
import com.fgr.service.MessageService;
import com.fgr.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class MessageController {
    @Autowired
    MessageService messageService;

    @Autowired
    UserService userService;

    @PostMapping("DetermineLoginMessage")
    @ResponseBody
    public String DetermineLoginMessage(HttpSession httpSession,Model model){
        Object obj = httpSession.getAttribute("userid");
        if (obj==null){
            return "Login";
        }
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

        httpSession.setAttribute("time",formatter.format(date));

        System.out.println(formatter.format(date));

        return "SUCCESS";

    }

    @GetMapping("findAllMessageBg")
    public String findAllMessageBg(Integer publish,Model model){
        List<Message> messages = messageService.selectAllMessage(publish);
        model.addAttribute("messages",messages);
        if (publish==1){
            return "background/message";
        }else {
            return "background/auditing-message";
        }
    }

    @GetMapping("findAllMessage")
    public String findAllMessage(Integer publish,Model model){
        List<Message> messages = messageService.selectAllMessage(publish);
        model.addAttribute("messages",messages);
        return "foreground/announcement";
    }

    @ResponseBody
    @PostMapping("deleteMessageByMsgIdBg")
    public String deleteMessageByMsgIdBg(Integer msgId){
        String s = messageService.deleteMessageByMsgIdBg(msgId);
        return s;
    }

    @GetMapping("selectUserAndMessageByMsgId")
    public String selectUserAndMessageByMsgId(Integer msgId,Model model){
        Message message = messageService.selectMessageByMsgId(msgId);
        List<User> users = userService.selectAllUser();
        model.addAttribute("message",message);
        model.addAttribute("users",users);
        return "background/modifymessage";
    }

    @GetMapping("selectMessageByMsgIdAuditing")
    public String selectMessageByMsgIdAuditing(Integer msgId,Model model){
        Message message = messageService.selectMessageByMsgIdAuditing(msgId);
        model.addAttribute("message",message);
        return "background/auditmessage";
    }

    @PostMapping("updateMessagePublishByMsgId")
    @ResponseBody
    public String updateMessagePublishByMsgId(Integer msgId){
        String s = messageService.updateMessagePublishByMsgId(msgId);
        return s;

    }

    @ResponseBody
    @PostMapping("updateMessage")
    public String updateMessage(Message message){
        String s = messageService.updateMessage(message);
        return s;

    }



    @ResponseBody
    @PostMapping("AddMessage")
    public String AddMessage(Message message, @RequestParam(value = "Img",required = false)MultipartFile Img) throws IllegalStateException,IOException {
        //1.获取上传文件对象
        String filename = Img.getOriginalFilename();
        //2.解决文件重名问题
        String substring = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID().toString() + substring;

        //3.获取服务器上的文件的路径
        //去服务器里面找到picture的图片路径
        String picturePath =new File("").getAbsolutePath() + File.separator + "..\\webapps\\picture";
        //输出路径
        System.out.println(picturePath);
        File file = new File(picturePath);
        //exists()检测文件是否存在
        if (!file.exists()) {
            //如果不存在创建文件
            file.mkdirs();
        }
        //拼接真正的文件路径
        //File.separator为获取路径中间的拼接符，windows中为//
        String realPath = picturePath + File.separator + filename;

        Img.transferTo(new File(realPath));


        //网络路径
        String path="http://116.204.113.19:8080/picture/"+filename;

        message.setMsgImg(path);
        message.setMsgPublish(1);

        String s = messageService.addMessage(message);
        return s;
    }

    @ResponseBody
    @PostMapping("AddMessageAuditing")
    public String AddMessageAuditing(Message message, @RequestParam(value = "Img",required = false)MultipartFile Img) throws IllegalStateException,IOException {
        //1.获取上传文件对象
        String filename = Img.getOriginalFilename();
        //2.解决文件重名问题
        String substring = filename.substring(filename.lastIndexOf("."));
        filename = UUID.randomUUID().toString() + substring;

        //3.获取服务器上的文件的路径
        //去服务器里面找到picture的图片路径
        String picturePath =new File("").getAbsolutePath() + File.separator + "..\\webapps\\picture";
        //输出路径
        System.out.println(picturePath);
        File file = new File(picturePath);
        //exists()检测文件是否存在
        if (!file.exists()) {
            //如果不存在创建文件
            file.mkdirs();
        }
        //拼接真正的文件路径
        //File.separator为获取路径中间的拼接符，windows中为//
        String realPath = picturePath + File.separator + filename;

        Img.transferTo(new File(realPath));


        //网络路径
        String path="http://116.204.113.19:8080/picture/"+filename;

        message.setMsgImg(path);
        message.setMsgPublish(0);

        String s = messageService.addMessage(message);
        return s;
    }

}
